import vnode from "./vnode";
import createElement from "./createElement";
import patchVNode from "./patchVNode";
export default function patch(oldNode,newNode) {
  // 判断老节点是虚拟节点还是DOM节点
  if(oldNode.sel === '' || oldNode.sel === undefined){
    // DOM节点，包装为虚拟节点
    oldNode = vnode(oldNode.tagName.toLowerCase(),{},[],undefined,oldNode)
  }
  // 判断oldNode，newNode是同一个节点
  if (oldNode.key === newNode.key && oldNode.sel === newNode.sel) {
    patchVNode(oldNode,newNode)
  } else {
    let newVnodeElm = createElement(newNode)
    if (oldNode.elm.parentNode && newVnodeElm) {
      oldNode.elm.parentNode.insertBefore(newVnodeElm,oldNode.elm);
    }
    oldNode.elm.parentNode.removeChild(oldNode.elm)
  }
}